package divestoclimb.checklist;

import divestoclimb.checklist.data.*;
import divestoclimb.checklist.storage.DbAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class EditItem extends Activity implements Button.OnClickListener {
	
	// The possible actions to pass to this activity
	// TODO: This parameter is unnecessary; it can be inferred by if an item
	// type is passed or not
	public static final int ACTION_NEW = 0;
	public static final int ACTION_EDIT = 1;

	// The action we are taking: new or edit
	protected int mAction;
	// If this is a new item, mId is the ID of the item we are inserting above or
	// 0L if we're inserting at the end.
	// Otherwise it's the ID of the item we are editing.
	protected long mId;
	// The type of item to create (Note or Check)
	protected int mType;
	// The type of container for this item (Template or Checklist)
	protected int mContainerType;
	// The ID of the container
	protected long mContainerId;

	// The item we're editing
	protected Item mItem;

	// The EditText field with the new text in it
	protected EditText mEditText;

	// This string is used to restore the text from a previous state.
	protected String mSavedText;

	protected DbAdapter mDbHelper;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		Bundle params = icicle != null? icicle: getIntent().getExtras();

		mDbHelper = new DbAdapter(this);

		mContainerType = params.getInt("containertype");
		mId = params.getLong(DbAdapter.KEY_ITEM_ID);
		mAction = params.getInt("action");
		if(mAction == ACTION_NEW) {
			mContainerId = params.getLong("containerid");
			mType = params.getInt(DbAdapter.KEY_ITEM_TYPE);
			mItem = null;
		}
		
		// Try to retrieve the text that was in the field before the activity was
		// stopped
		if(icicle != null) {
			mSavedText=icicle.getString(DbAdapter.KEY_ITEM_TEXT);
		}
		
		setContentView(R.layout.rename_dialog);
		
		mEditText = (EditText)findViewById(R.id.content);
		
		// Set up the button listener
		Button ok = (Button)findViewById(R.id.button_ok);
		ok.setOnClickListener(this);
		Button cancel = (Button)findViewById(R.id.button_cancel);
		cancel.setOnClickListener(this);
	}

	@Override
	public void onResume() {
		super.onResume();
		mDbHelper.open();
		if(mAction == ACTION_EDIT) {
			// Look up the item we're editing
			if(mContainerType == DbAdapter.ITEM_CONTAINER_TEMPLATE) {
				mItem = mDbHelper.fetchTemplateItem(mId);
			} else {
				mItem = mDbHelper.fetchChecklistItem(mId);
			}
			if(mItem instanceof Check) {
				mType = DbAdapter.ITEM_TYPE_CHECK;
			} else {
				mType = DbAdapter.ITEM_TYPE_NOTE;
			}
			
			// Populate the EditText with the existing text from this item, or
			// the saved text from the previous instance if the activity was stopped
			// and resumed
			mEditText.setText(mItem.getText());
		}
		if(mSavedText != null) {
			mEditText.setText(mSavedText);
		}

		// Initialize the TextView description
		TextView prompt = (TextView)findViewById(R.id.prompt);
		String typeText = getResources().getString((mType == DbAdapter.ITEM_TYPE_CHECK)? R.string.check: R.string.note); 
		prompt.setText(String.format(getResources().getString(R.string.edit_item_prompt), typeText));
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putLong("containertype", mContainerType);
		outState.putLong(DbAdapter.KEY_ITEM_ID, mId);
		outState.putLong("action", mAction);
		if(mAction == ACTION_NEW) {
			outState.putLong("containerid", mContainerId);
			outState.putLong(DbAdapter.KEY_ITEM_TYPE, mType);
		}
		outState.putString(DbAdapter.KEY_ITEM_TEXT, mEditText.getText().toString());
	}

	@Override
	protected void onPause() {
		super.onPause();
		mDbHelper.close();
	}

	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.button_cancel:
			setResult(RESULT_CANCELED);
			finish();
			break;
		case R.id.button_ok:
			if(mItem == null) {
				// We must be creating a new Item
				if(mType == DbAdapter.ITEM_TYPE_CHECK) {
					mItem = new Check(mContainerType, mContainerId);
				} else {
					mItem = new Note(mContainerType, mContainerId);
				}
			}
			mItem.setText(mEditText.getText().toString());

			switch(mAction) {
			case ACTION_NEW:
				// We need to set the order on the Item before
				// we can save
				ItemList itemOrderHelper = new ItemList(mContainerType, mContainerId, mDbHelper);
				itemOrderHelper.insertAbove(mId, mItem);
				itemOrderHelper.getCursor().close();
				break;
			case ACTION_EDIT:
				// We don't have to change anything else, just call save()
				mItem.save(mDbHelper);
				break;
			}
			setResult(RESULT_OK);
			finish();
			break;
		}
	}
}